
package com.compuvest.tld;

import javax.servlet.jsp.JspException;
import javax.servlet.jsp.JspTagException;
import javax.servlet.jsp.tagext.TagSupport;

import java.util.List;
import java.util.ArrayList;
import java.util.Map;

import com.compuvest.web.Category;
import com.compuvest.web.Items;
import com.compuvest.web.Preference;
import java.net.URLEncoder;

public class LeftMenu extends TagSupport {

//        "<nobr>Price Range</nobr>" +
//        "<ul><li class=mr onClick=\"cv_togls(99,1)\" id=b991 style='display:block;'/>Set Price Range</ul>" +
//        "<div id=b990 style='display:none;'>" +
//        " $<input type=text name='lo' size=4 value='0' > - $<input type=text name='hi' size=4 value='0'>" +
//        " <input type=submit name='range' value='Go'>" +
//        "<ul><li class=hd onClick=\"cv_togls(99,0)\"/>Hide Price Range</ul>" +
//        "</div>";

    private String pageName;
    private int categoryID;
    private int subcategoryID;
    private int lowPrice;
    private int hiPrice;
    private String pageUrl;
    private StringBuilder menu;
    private Items items;
    private String popUrl;
    private Map<Integer,String> popData;

//----------------------------
//
    @Override
    public int doStartTag() throws JspException {

//        long startEvent = System.currentTimeMillis();

//        if(items.size() == 0) return SKIP_BODY;

        menu = new StringBuilder(11000);
        try {
            menu.append("<div id='cv_menu'>");
            menu.append("<div class='mn_tp'><img src='../images/h/lup.gif'/></div>");

            menu.append("<div id=cv_wm>");
            // category
            if(categoryID == 0) {
                menu.append("<span>").append(pageName).append("</span>");
                menu.append("<hr class='hrs'/>");
                menu.append("<ul>");
                for(Category catMenu : items.getCategories()) {
                    menu.append("<li/><a href='").append(pageUrl).append("&cid=").append(catMenu.getCategoryID());
                    menu.append("'>").append(catMenu.getName()).append("</a> (").append(catMenu.getItemsInCategory()).append(")");
                }
                menu.append("</ul>");
            }
            // subcategory
            else if(categoryID != 0 && subcategoryID == 0) {
                menu.append("<span>").append(items.getCategoryName()).append("</span>");
                menu.append("<hr class='hrs'/>");
                menu.append("<ul>");
                for(Category scatMenu : items.getSubcategories()) {
                    menu.append("<li/><a href='").append(pageUrl).append("&cid=").append(categoryID);
                    menu.append("&sid=").append(scatMenu.getCategoryID());
                    menu.append("'>").append(scatMenu.getName()).append("</a> (").append(scatMenu.getItemsInCategory()).append(")");
                }
                menu.append("</ul>");
            }
            // preference
            else if(categoryID != 0 && subcategoryID != 0) {
                menu.append("<span>Search by preference</span>");

                if(items.getPages() > 1) { // not show if one page
                    menu.append("<hr class='hrs'/>");
                    menu.append("<nobr>Price Range</nobr><div>");
                    menu.append("$<input type=text name=lop size=4 value=").append(lowPrice).append(" /> - ");
                    menu.append("$<input type=text name=hip size=4 value=").append(hiPrice).append(" />");
                    menu.append(" <input type=submit name=range value='Go'/>");
                    menu.append("</div>");
                }

                ArrayList<Preference> preferenceArray = items.getPreference();
                for(int indexPreference = 0; indexPreference < preferenceArray.size(); indexPreference++) {

                    List<String> preferenceBlock = preferenceArray.get(indexPreference).getBlock();
                    if(preferenceBlock == null || preferenceBlock.size() == 1) continue; // if 1 no need

                    menu.append("<hr/>");

                    menu.append("<nobr>").append(preferenceArray.get(indexPreference).getTitle()).append("</nobr>");

                    String end = "";
                    menu.append("<ul>");
                    for(int idxName = 0; idxName < preferenceBlock.size(); idxName++) {
                        menu.append("<li/><a href='").append(pageUrl);
                        menu.append("&cid=").append(categoryID).append("&sid=").append(subcategoryID);
                        menu.append("&lop=").append(lowPrice).append("&hip=").append(hiPrice);
                        menu.append("&p").append(indexPreference).append("=");
                        menu.append(URLEncoder.encode(preferenceBlock.get(idxName), "UTF-8")).append(popUrl).append("'>");
                        menu.append(preferenceBlock.get(idxName)).append("</a> (");
                        menu.append(preferenceArray.get(indexPreference).getCount(preferenceBlock.get(idxName)));
                        menu.append(")");

                        // view less 5
                        if(idxName == 5) {
                            menu.append("<li class=mr onClick='cv_togls(").append(indexPreference).append(",1)' id=b");
                            menu.append(indexPreference).append("1 style='display:block;'/>More ...");
                            menu.append("</ul>");
                            menu.append("<ul id=b").append(indexPreference).append("0 style='display:none;'>");

                            end = "<li class='hd' onClick='cv_togls(" + indexPreference + ",0)'>Hide</li>";
                        }
                    }
                    menu.append(end);
                    menu.append("</ul>");
                }
            }
            menu.append("</div>");

            menu.append("<div class=mn_ft><img src='../images/h/ldw.gif'></div>");
            menu.append("</div>");

            pageContext.getOut().print(menu.toString());
//            pageContext.getOut().print(menu.toString() +"<br>"+menu.length()+" t="+(int)(System.currentTimeMillis()-startEvent)+" lp="+lowPrice+" hp="+hiPrice);
        }
        catch(java.io.IOException e) {
            throw new JspTagException("com.compuvest.tlg.LeftMenu: " + e.getMessage());
        }

        return SKIP_BODY;
    }
    
//----------------------------
//
    @Override
    public int doEndTag() {
        return EVAL_PAGE;
    }

//----------------------------
//
    @Override
    public void release() {
        pageName = null;
        pageUrl = null;
        menu = null;
        items = null;
        popUrl = null;
        popData = null;
    }


//----------------------------
//
    public void setPageName(String pageName)                    { this.pageName = pageName; }
    public void setCategoryID(int categoryID)                   { this.categoryID = categoryID; }
    public void setSubcategoryID(int subcategoryID)             { this.subcategoryID = subcategoryID; }
    public void setLowPrice(int lowPrice)                       { this.lowPrice = lowPrice; }
    public void setHiPrice(int hiPrice)                         { this.hiPrice = hiPrice; }
    public void setPageUrl(String pageUrl)                      { this.pageUrl = pageUrl; }
    public void setItems(Items items)                           { this.items = items; }
    public void setPopUrl(String popUrl)                        { this.popUrl = popUrl; }
    public void setPopData(Map<Integer,String> popData)         { this.popData = popData; }
}
